#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re

import xlwt

# 读取语言文件路径文件路径
string_file = '../strings.xml'
# 生成表格文件路径
save_excel_path = '../strings_excel.xls'

pattern = r'<string[\s]+name[\s]?="(?P<key>\w+)"[\s]?[\s\S]*>(?P<value>[\s\S]+)</string>[\s]?'


def split_str():
    f = open(string_file, 'r', encoding='UTF-8')
    dir = {}

    line = f.readline()
    while line:
        if len(line) <= 1:
            line = f.readline()
            continue
        nameSearch = re.search(pattern, line, 0)

        if nameSearch is not None and nameSearch.group('key') is not None and nameSearch.group('value') is not None:
            # print(nameSearch.group('key'),nameSearch.group('value'))
            dir[nameSearch.group('key')] = nameSearch.group('value')
        line = f.readline()
    # // pattern1 = r'name="[a-zA-Z_]*"'
    f.close()
    return dir


def set_style(name='Times New Roman', height=220, bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font
    font.bold = bold
    font.name = name
    font.colour_index = 4
    font.height = height
    style.font = font
    return style


def create_excel(sheet_name='chinese'):
    dir = split_str()
    if len(dir) > 0:
        exc = xlwt.Workbook(encoding='utf-8')
        sheet = exc.add_sheet(sheet_name, True)
        keys = list(dir.keys())
        sheet.write(0, 0, "key")
        sheet.write(0, 1, "中文")
        sheet.write(0, 2, "日文")
        for index in range(0, len(keys)):
            # print index
            # print keys[index]
            # print dir.get(keys[index])

            sheet.write(index + 1, 0, keys[index])
            sheet.write(index + 1, 1, dir.get(keys[index]))

        exc.save(save_excel_path)


# 主函数
def main():
    # read_file()
    # split_str()
    str = input("请输入表名：")
    if len(str) <= 0:
        str = "chinese"
    create_excel(str)


if __name__ == "__main__":
    main()
